﻿using System;
using System.Windows.Forms;
using DC2010.Objects.Data;
using DC2010.Tools;
using DC2010;

namespace DC2010_EditorInfo
{
    public partial class ItemsForm : Form
    {
        public ItemInfo ItemInfo { get; set; }
        public string OriginalId { get; set; }


        public ItemsForm()
        {
            InitializeComponent();

            ItemInfo = new ItemInfo();
        }

        private void ItemsForm_Load(object sender, EventArgs e)
        {
            InitializeLB();
        }

        private void InitializeLB()
        {
            imageListBox1.Items.Clear();

            foreach (ItemInfo ei in Root.I.ItemSystem.DictInfo.Values)
            {

                ImageListBoxItem ilbi = new ImageListBoxItem(ei.Id, Properties.Resources.Item);
                imageListBox1.Items.Add(ilbi);
            }
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(OriginalId))
            {
                //we are updating
                GatherInfo(ItemInfo);
                Root.I.ItemSystem.SaveInfo();
                InitializeLB();
            }
            else
            {
                //creating new
                GatherInfo(ItemInfo);
                Root.I.ItemSystem.AddInfo(ItemInfo);
                Root.I.ItemSystem.SaveInfo();
                InitializeLB();
            }
        }

        private void GatherInfo(ItemInfo ei)
        {
            ei.Id = IdTB.Text;
            ei.ShortName = ShortTB.Text;
            ei.LongName = DescriptionTB.Text;
            ei.DivinePoints = DivinePointsTB.Text.ToInt();
            ei.Conditions = keyValueUC2.Data;
            ei.Params = keyValueUC3.Data;
            ei.GfxId = textBox1.Text;

        }

        private void FillWithObject()
        {
            if (ItemInfo != null)
            {
                OriginalId = ItemInfo.Id;
                IdTB.Text = ItemInfo.Id;
                ShortTB.Text = ItemInfo.ShortName;
                DescriptionTB.Text = ItemInfo.LongName;
                DivinePointsTB.Text = ItemInfo.DivinePoints.ToString();
                textBox1.Text = ItemInfo.GfxId;

                keyValueUC2.Data = ItemInfo.Conditions;
                keyValueUC3.Data = ItemInfo.Params;

                keyValueUC1.FillList();
                keyValueUC2.FillList();
                keyValueUC3.FillList();

            }
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = ((ImageListBoxItem)imageListBox1.SelectedItem).Text;
            ItemInfo = Root.I.ItemSystem.GetInfo(s);
            FillWithObject();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            ItemInfo = new ItemInfo();
            OriginalId = "";
        }
    }
}
